
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

#include "widgetconfigcomm.h"
#include "config.h"

#include <QtGlobal>
#if QT_VERSION >= 0x050000
#include <QtWidgets/QtWidgets>
#endif

WidgetConfigComm::WidgetConfigComm(QObject *parent)
	: QObject(parent)
{
}

WidgetConfigComm::~WidgetConfigComm()
{
}

bool WidgetConfigComm::SaveWidgetConfig(const QWidget * widgetParent, const QWidget * widgetConfigGroup)
{
	bool bRet = false;

	do
	{
		if (!widgetParent)
			break;
		if (!widgetConfigGroup)
			break;
		if (widgetConfigGroup->objectName().isNull() || widgetConfigGroup->objectName().isEmpty())
			break;
		bRet = SaveWidgetConfig(widgetParent, widgetConfigGroup->objectName());
	} while (false);

	return bRet;
}

bool WidgetConfigComm::SaveWidgetConfig(const QWidget * widgetParent, const QString szConfigGroup)
{
	bool bRet = false;

	do 
	{
		if (!widgetParent)
			break;

		if (szConfigGroup.isNull() || szConfigGroup.isEmpty())
			break;

		const QObjectList list = widgetParent->children();

		if (list.empty())
			break;

		QStringList qslKeys, qslValues;

		foreach(QObject* obj, list)
		{
			if (obj->objectName().isNull() || obj->objectName().isEmpty())
				continue;

			QString szClassName = (obj->metaObject()->className());
			if (szClassName == "QCheckBox")
			{
				QCheckBox* cb = static_cast<QCheckBox*>(obj);
				qslKeys << cb->objectName();
				qslValues << (cb->isChecked() ? "1" : "0");
                if ( cb->objectName() == "cbAutoForSend" )
                    continue;
			}
			else if (szClassName == "QLineEdit")
			{
				QLineEdit* le = static_cast<QLineEdit*>(obj);				
				qslKeys << le->objectName();
				qslValues << le->text();
			}
			else if (szClassName == "QComboBox")
			{
				QComboBox* comboBox = static_cast<QComboBox*>(obj);
				qslKeys << comboBox->objectName() + "No";
				qslValues << QString::number(comboBox->currentIndex());
				qslKeys << comboBox->objectName();
				qslValues << comboBox->currentText();
			}
			else if (szClassName == "QRadioButton")
			{
				QRadioButton* rb = static_cast<QRadioButton*>(obj);
				qslKeys << rb->objectName();
				qslValues << (rb->isChecked() ? "1" : "0");
			}
			else if (szClassName == "QSlider")
			{
				QSlider* slider = static_cast<QSlider*>(obj);
				qslKeys << slider->objectName();
				qslValues << QString::number(slider->value());
			}
			else if (szClassName == "QSpinBox")
			{
				QSpinBox* spinBox = static_cast<QSpinBox*>(obj);
				qslKeys << spinBox->objectName();
				qslValues << QString::number(spinBox->value());
			}
			else
			{
				continue;
			}
		}

		saveConfig(szConfigGroup, qslKeys, qslValues);

		bRet = true;
	} while (false);

	return bRet;;
}

bool WidgetConfigComm::LoadWidgetConfig(QWidget* widgetParent, QWidget* widgetConfigGroup)
{
	bool bRet = false;

	do
	{
		if (!widgetParent)
			break;

		if (!widgetConfigGroup)
			break;

		if (widgetConfigGroup->objectName().isNull() || widgetConfigGroup->objectName().isEmpty())
			break;

		bRet = LoadWidgetConfig(widgetParent, widgetConfigGroup->objectName());
	} while (false);

	return bRet;
}

bool WidgetConfigComm::LoadWidgetConfig(QWidget* widgetParent, QString szConfigGroup)
{
	bool bRet = false;

	do 
	{
		if (!widgetParent)
			break;

		if (szConfigGroup.isNull() || szConfigGroup.isEmpty())
			break;

		const QObjectList list = widgetParent->children();

		if (list.empty())
			break;

		QStringList qslKeys;

		foreach(QObject* obj, list)
		{
			if (obj->objectName().isNull() || obj->objectName().isEmpty())
				continue;

			QString szClassName = (obj->metaObject()->className());
			if (szClassName == "QCheckBox")
			{
				QCheckBox* cb = static_cast<QCheckBox*>(obj);
				qslKeys << cb->objectName();
                if ( cb->objectName() == "cbAutoForSend" )
                    continue;
			}
			else if (szClassName == "QLineEdit")
			{
				QLineEdit* le = static_cast<QLineEdit*>(obj);
				qslKeys << le->objectName();
			}
			else if (szClassName == "QComboBox")
			{
				QComboBox* comboBox = static_cast<QComboBox*>(obj);
				qslKeys << comboBox->objectName() + "No";
				qslKeys << comboBox->objectName();
			}
			else if (szClassName == "QRadioButton")
			{
				QRadioButton* rb = static_cast<QRadioButton*>(obj);
				qslKeys << rb->objectName();
			}
			else if (szClassName == "QSlider")
			{
				QSlider* slider = static_cast<QSlider*>(obj);
				qslKeys << slider->objectName();
			}
			else if (szClassName == "QSpinBox")
			{
				QSpinBox* spinBox = static_cast<QSpinBox*>(obj);
				qslKeys << spinBox->objectName();
			}
			else
			{
				continue;
			}
		}

		QMap<QString, QString> mapValues = loadConfig(szConfigGroup, qslKeys, 0);

		if (mapValues.size() <= 0)
			break;

		foreach(QObject* obj, list)
		{
			if (obj->objectName().isNull() || obj->objectName().isEmpty())
				continue;

			QString szClassName = (obj->metaObject()->className());
			if (szClassName == "QCheckBox")
			{
				QCheckBox* cb = static_cast<QCheckBox*>(obj);
				cb->setChecked(mapValues[cb->objectName()] == "1" ? true : false);
			}
			else if (szClassName == "QLineEdit")
			{
				QLineEdit* le = static_cast<QLineEdit*>(obj);
				le->setText(mapValues[le->objectName()]);
			}
			else if (szClassName == "QComboBox")
			{
				QComboBox* comboBox = static_cast<QComboBox*>(obj);
				comboBox->setCurrentText(mapValues[comboBox->objectName()]);
			}
			else if (szClassName == "QRadioButton")
			{
				QRadioButton* rb = static_cast<QRadioButton*>(obj);
				rb->setChecked(mapValues[rb->objectName()] == "1" ? true : false);
			}
			else if (szClassName == "QSlider")
			{
				QSlider* slider = static_cast<QSlider*>(obj);
				slider->setValue(mapValues[slider->objectName()].toInt());
			}
			else if (szClassName == "QSpinBox")
			{
				QSpinBox* spinBox = static_cast<QSpinBox*>(obj);
				spinBox->setValue(mapValues[spinBox->objectName()].toInt());
			}
			else
			{
				continue;
			}
		}

		bRet = true;
	} while (false);

	return bRet;
}
